<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
    Integer uid = input.getInt("uid", token.getUid());
    List<User> users = QBuilder.db("basedb").select().from("User").where(new Where("hospital=", hospital).and("status = ", STATUS_ENABLE).and("id=", uid)).query().getResultObj();
    if (isEmpty(users)) {
        msg = new Message(30041, "用户不存在");
        Tools.encryptPrint(out, msg);
        return;
    }
    User u = users.get(0);
    if (u.getType() == 200) {
        Integer age = input.getInt("age", -1);
        Integer gender = input.getInt("gender", Tools.getInt(u.getGender() + "", -1));
        String title  = input.getDesc("title", Tools.getDesc(u.getTitle(), ""));
        String position = input.getDesc("position", Tools.getDesc(u.getJson("position"), ""));
        String introduce = input.getDesc("introduce", Tools.getDesc(u.getJson("introduce"), ""));
        String teachTitle = input.getDesc("teach_title", Tools.getDesc(u.getJson("teach_title"), ""));
        String time = input.getDesc("time", "");
		String phone = input.getDesc("phone", Tools.getDesc(u.getPhone(), ""));
		List<User> list = QBuilder.db("basedb").select().from("User as u").where(new Where("u.status=", STATUS_ENABLE).and("u.hospital=", hospital).and("(u.userName = '" + phone + "' or u.phone='" + phone + "')").and("u.type=", USER_EMPLOYEE).and("u.id!=", uid)).page(1, 1).query().getResultObj();
		if (!isEmpty(list)) {
			 Tools.encryptPrint(out, new Message(30031, "该手机号已经绑定了其他账户"));
			 return;
		}
		if (time.equals("")) {
			time = Tools.getDesc(JSONUtil.toJson(u.getJson("time")), "{}");
		} else {
			time = Tools.decryptStr(time);
		}
        String specialty = input.getDesc("specialty", Tools.getDesc(u.getJson("specialty"), ""));
		u.setTitle(filterAll(title));
		u.setPhone(phone);
		u.setJson("position", filterAll(position));
		u.setJson("introduce", filterAll(introduce));
		u.setJson("teach_title", filterAll(teachTitle));
		Map<String, Map> timeMap = JSONUtil.fromJson(time, new TypeReference<Map<String, Map>>(){});
		if (timeMap == null)
			timeMap = new HashMap();
		u.setJson("time", timeMap);
		u.setJson("specialty", filterAll(specialty));
		if (gender != -1)
			u.setGender(gender);
		if (age > 0) {
			if (age > 300) {
				Tools.encryptPrint(out, new Message(30032, "年龄不可大于300"));
				return;
			}
            u.setBirthday(age2birth(age));
        }	
        DBTools.db("basedb").modItem(u, u.getId());
    } else {
        User user = User.fromMap(parameters, User.class);
        user.setId(uid);
		user.setJson(u.getJson());
        UserProfile userProfile = UserProfile.fromMap(parameters, UserProfile.class);
        userProfile.setUid(uid);
        msg = complete(user, userProfile);
    }
    Tools.encryptPrint(out, msg);
%>
